package com.system.provider;

import java.util.Map;

import com.am.ui.grid.AbstractBaseGridProvider;
import com.system.uitls.CommonUtil;
import com.system.uitls.SearchFilter;

public class DrugUseHistoryListProvider extends AbstractBaseGridProvider
{
  public String generateSql(Map<String, Object> map)
  {
    String startTime = CommonUtil.nullToEmptyString(map.get("startTime"));
    String endTime = CommonUtil.nullToEmptyString(map.get("endTime"));
    String type = CommonUtil.nullToEmptyString(map.get("type"));
    String place = CommonUtil.nullToEmptyString(map.get("place"));
    String status = CommonUtil.nullToEmptyString(map.get("status"));
    StringBuffer sql;
    if (Integer.parseInt(place) == 1)
    {
      sql = new StringBuffer("SELECT t1.id,t3.name,t1.quantity,t1.type,t2.user_name,t2.create_time,t2.notes,t1.status,(select value from sys_data_dictionary t where t.id = t2.outCompany) as outCompany FROM sys_drug_history t1 ,sys_out_storage t2,sys_drug t3 WHERE t1.out_storage_id = t2.id AND t1.drug_id = t3.id AND t1.type in(0,1,2,3,4,5)");
    }
    else
    {
      sql = new StringBuffer("SELECT t1.id,t3.name,t1.quantity,t1.type,t2.user_name,t2.create_time,t2.notes,t1.status,(select value from sys_data_dictionary t where t.id = t2.outCompany) as outCompany FROM sys_drug_history t1 ,sys_out_storage t2,sys_drug t3 WHERE t1.out_storage_id = t2.id AND t1.drug_id = t3.id AND t1.type in(1,4,6)");
    }

    if (!"".equals(startTime)) {
      sql.append(" AND t2.create_time>=:startTime");
    }

    if (!"".equals(endTime)) {
      sql.append(" AND t2.create_time<=:endTime");
    }

    if (!"".equals(type)) {
      sql.append(" AND t1.type=:type");
    }

    if (!"".equals(status)) {
      sql.append(" AND t1.status=:status");
    }

    sql.append(SearchFilter.getSearchSql(map, null));
    setParamMap(map);
    return sql.toString();
  }
}